﻿using DevExpress.ExpressApp;
using EasyXaf.EntityDesigner.BusinessObjects.Entities;

namespace EasyXaf.EntityDesigner.Controllers;

public class EntityViewController : ViewController<DetailView>
{
    protected override void OnActivated()
    {
        base.OnActivated();

        ObjectSpace.Committing += ObjectSpace_Committing;
    }

    protected override void OnDeactivated()
    {
        ObjectSpace.Committing -= ObjectSpace_Committing;

        base.OnDeactivated();
    }

    private void ObjectSpace_Committing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (View.CurrentObject is Entity entity)
        {
            var entityDesign = ObjectSpace.FirstOrDefault<EntityDesign>(e => e.Entity == entity);
            if (entityDesign == null)
            {
                entity.SetLocation([0, 0, 0, 0], entity.DomainModel);
            }
        }
    }
}
